#ifndef __WARD_HTTP_SERVER_H__
#define __WARD_HTTP_SERVER_H__

#include <QTcpServer>
#include <QTcpSocket>
#include <qdatetime.h>

#ifdef __USE_CUDA_WARD_SENSE__
#include "cuda_manager.h"
#include "ai_recognition.h"
#endif

struct ClientInfo {
    QByteArray buffer;
    bool headersParsed = false;
    QByteArray boundary;
    QByteArray method;
    QByteArray path;
    QByteArray contentType;
    qint64 contentLength = 0;
};

class WardHttpServer : public QTcpServer {
    Q_OBJECT
public:
    explicit WardHttpServer(QObject *parent = nullptr);
    QHash<QTcpSocket*, ClientInfo> clientInfoMap;
    bool _in_multipart;
    bool multipart;
    QByteArray _buffer;
    QByteArray _boundary;

protected:
    void incomingConnection(qintptr socketDescriptor) override;

private slots:
    QByteArray extractBoundary(const QByteArray& request);
    QByteArray processMultipartData(const QByteArray& request);
    QByteArray GetSubtitles(QDateTime start_time, QDateTime end_time);
    void handleRequest();
    void sendResponse(QTcpSocket* client, int status, const QByteArray& content);

private:
    QByteArray processImage(const QByteArray& imageData);

    int last_time;
};

class HttpServerThread : public QThread {
    Q_OBJECT

public:
    explicit HttpServerThread(QObject *parent = nullptr);
    ~HttpServerThread();

    void run() override;

private:
    WardHttpServer* httpServer;
};

#endif // __WARD_HTTP_SERVER_H__
